package jeesample.service.publishers;

import java.util.LinkedList;
import java.util.List;

import jeesample.dtos.RoomDTO;
import jeesample.model.Room;
import jeesample.service.RoomLocatorService;
import jeesample.service.impl.RoomLocatorImpl;

import org.apache.log4j.Logger;

public class LocatorServicePublisher extends BaseServicePublisher {
	private Logger logger = Logger.getLogger(getClass());

	@Override
	protected Logger logger() {
		return logger;
	}

	private RoomLocatorService roomLocatorService = new RoomLocatorImpl();

	public List<RoomDTO> listAllRooms() {
		dbg("listAllRooms()");
		List<Room> rooms = roomLocatorService.getAllRooms();
		List<RoomDTO> dtos = new LinkedList<>();
		for (Room room : rooms) {
			RoomDTO dto = new RoomDTO(room);
			dtos.add(dto);
		}
		return dtos;
	}

	public List<RoomDTO> listRoomsIn(String location) {
		dbg("listRoomsIn(" + location + ")");
		List<Room> rooms = roomLocatorService.findRoomIn(location);
		List<RoomDTO> dtos = new LinkedList<>();
		for (Room room : rooms) {
			RoomDTO dto = new RoomDTO(room);
			dtos.add(dto);
		}
		return dtos;
	}

	public List<String> listAllLocations() {
		dbg("listAllLocations()");
		return roomLocatorService.getAllLocations();
	}

	public RoomDTO addRoom(RoomDTO dto) {
		dbg("addRoom:" + dto + ")");
		Room room = dto.getModel();
		room = roomLocatorService.add(room);
		RoomDTO newDto = new RoomDTO(room);
		return newDto;
	}

	public String deleteRoom(RoomDTO dto) {
		dbg("deleteRoom:(" + dto + ")");
		Room room = dto.getModel();
		String val = roomLocatorService.delete(room);
		return val;
	}

}
